<template>
  <div class="goods">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片组件 -->
    <el-card>
      <!-- 布局 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索 -->
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 添加按钮 -->
        <el-col :span="5">
          <el-button type="primary" @click="goodsAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="80"
        ></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150">
          <template slot-scope="scoped">{{ scoped.row.add_time }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scoped">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoods(scoped.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :page-sizes="[10, 20, 30, 40]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      total: 0
    };
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      });
      // console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("获取商品列表失败");
      // 如果获取
      this.goodsList = res.data.goods;
    },
    // 添加商品列表
    // 删除
    async delGoods(id) {
      let dd = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(error => error);
      // console.log(dd);
      if (dd == "cancel") return this.$message("取消删除");
      if (dd == "confirm") {
        // eslint-disable-next-line no-unused-vars
        let { data: res } = await this.$http.delete("categories/" + id);
        this.getGoodsList();
        // console.log(res)
      }
    },
    // 点击添加商品按钮
    goodsAdd() {
      this.$router.push("/goods/add");
    }
  },
  created() {
    this.getGoodsList();
  }
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 15px;
  margin-bottom: 15px;
}

.el-table {
  margin-top: 20px;
  font-size: 12px;
}
.el-pagination {
  margin-top: 15px;
}
</style>
